<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="keywords" content="Huooo">
    <meta name="description" content="Huooo">
    <meta name="author" content="Huooo">
    <title>jsapi权限验证配置流程-免登流程</title>
    <link rel=" shortcut icon" href="">
    <!-- css files -->
    <!-- css style -->
	<link type="text/css" rel="stylesheet" href="stylesheets/style.css" />
	<style>
	body {
		background-color: white;
	}
	
	* {
		padding: 0;
		margin: 0;
	}
	
	ul {
		list-style: none
	}
	
	li {
		height: 70px;
		padding: 10px;
		border-bottom: 1px solid #ccc;
		vertical-align: middle;
	}
	
	.icon img {
		height: 70px;
		width: 70px;
	}
	
	.icon {
		display: inline-block;
		vertical-align: middle;
		/*border: 1px solid #00ff00;*/
	}
	
	.text {
		margin-left: 10px;
		width: calc(100% - 50px);
		display: inline-block;
		text-align: left;
		line-height: 70px;
		vertical-align: middle;
	}
	</style>
<!-- js files 调用jquery需要的库，手机版和电脑版的不同 -->
<script type="text/javascript" src='http://lib.baomitu.com/zepto/1.1.7/zepto.min.js'></script>
<!-- 手机版钉钉免登引入的jsapi,和电脑版引入的不同 -->
<script type="text/javascript" src="https://g.alicdn.com/dingding/open-develop/1.5.1/dingtalk.js"></script>
<!-- js script 获取code码值的js文件 -->
<script>
$(document).ready(function () {
    var url = window.location.href;
    console.log("url", url);
    var corpId = "ding00b1a56210609a0835c2f4657eb6378f";  // 企业的corpId
    var signature = "";
    var nonceStr = "";
    var timeStamp = "";
    var agentId = "";
    $.post('/jsApi/get_js_config',{
            "url": url,
            "corpId": corpId
        },
        function (result) {
            console.log("result", result);
            signature = result.signature;
            nonceStr = result.nonceStr;
            timeStamp = result.timeStamp;
            agentId = result.agentId;
            corpId = result.corpId;
            dd.config({
                agentId: agentId,
                corpId: corpId,
                timeStamp: timeStamp,
                nonceStr: nonceStr,
                signature: signature,
                jsApiList: [
                    'runtime.info',
                    'biz.contact.choose',
                    'device.notification.confirm',
                    'device.notification.alert',
                    'device.notification.prompt',
                    'biz.ding.post',
                    'biz.util.openLink'] //必填，需要使用的jsapi列表
            });
            dd.ready(function () {
                   // console.log('dd.ready rocks!')
                    dd.runtime.info({
                        onSuccess: function (info) {
                            console.log('runtime info: ' + JSON.stringify(info));
                            alert(JSON.stringify(info));
                        },
                        onFail: function (err) {
                            console.log('fail: ' + JSON.stringify(err));
                            alert(JSON.stringify(err));
                        }
                    });
                    
                    //获取微应用免登授权码
                    dd.runtime.permission.requestAuthCode({
                        corpId: corpId, //企业id
                        onSuccess: function (info) {
                            console.log('authcode' + info.code);
                            alert('authcode = '+info.code);
                            Window.authcode = info.code;   //免登授权码
                            
                            //请求后台通过code值获得userId
                			$.ajax({
                				url : 'userinfo?code=' + info.code + '&corpid='+ corpId,
                				type : 'GET',
                				success : function(data, status, xhr) {
                				    alert(data);
                					var info = JSON.parse(data);
                					document.getElementById("userName").innerHTML = info.name;
                					document.getElementById("userId").innerHTML = info.userid;                					
                					// 图片
                					if(info.avatar.length != 0){
                			            var img = document.getElementById("userImg");
                			            img.src = info.avatar;
                			                      img.height = '100';
                			                      img.width = '100';
                			          }

                				},
                				error : function(xhr, errorType, error) {
                					logger.e("yinyien:" + _config.corpId);
                					alert(errorType + ', ' + error);
                				}
                			});                        
                            
                        },
                        onFail: function (err) {
                            console.log('requestAuthCode fail: ' + JSON.stringify(err));
                            alert(JSON.stringify(err));
                        }
                    });
                }
            );
        })
});
</script>
<script>    
</script>    
</head>
<body>
<div align="center">
		<img id="userImg" alt="头像" src="./nav/default.png">
</div>
<div align="center">
	<span>UserName:</span>
	<div id="userName" style="display: inline-block"></div>
</div>
<div align="center">
	<span>UserId:</span>
	<div id="userId" style="display: inline-block"></div>
</div>
</body>
</html>